package Model;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JOptionPane;

/**
 * Describe contact data along with its group and its creation date
 * 
 * @author Vincent Francolin
 * @version 1.0 : creation
 */
public class ContactRecord {
	int id;
	private Contact contact;
	private Group group;
	private Date addDate;
	private boolean isLoadInMemory;
	private String filename;
	private Date date;

	/**
	 * default constructor
	 * 
	 * @param id
	 *            id of the contact (unused)
	 * @param contact
	 *            the data of the contact
	 * @param group
	 *            the group where the contact belongs
	 * @param addDate
	 *            the date of the creation of the contact
	 */
	public ContactRecord(int id, Contact contact, Group group, Date addDate) {
		this.id = id;
		this.contact = contact;
		this.group = group;
		this.addDate = addDate;
		isLoadInMemory = true;
		filename = null;
		date = null;
	}

	/**
	 * constructor which create a contact from a file
	 * 
	 * @param filename
	 *            the name of the file
	 * @param g
	 *            group the contact belongs to
	 */
	public ContactRecord(String filename, Group g) {
		this.filename = filename;
		date = null;
		isLoadInMemory = false;
		// loadContact(filename);

		if (g != null){
			group = g;
			System.out.println("create contact with group : "+group.getName());
		}
		else
			group = null;
		// System.out.println("received : "+group.getName());
	}

	/**
	 * constructor which create a contact from a file
	 * 
	 * @param filename
	 *            the name of the file
	 * @param g
	 *            group the contact belongs to
	 * @param date
	 *            date when the contact was created
	 */
	public ContactRecord(String filename, Group g, Date date) {
		this.filename = filename;
		this.date = date;
		// loadContact(filename,date);
		group = g;
	}

	public Group getGroup() {
		loadWhenNeeded();
		return group;
	}

	public void setContact(Contact contact) {
		loadWhenNeeded();
		this.contact = contact;
	}

	public void setGroup(Group group) {
		loadWhenNeeded();
		this.group = group;
	}

	public Contact getContact() {
		loadWhenNeeded();
		return contact;
	}

	public Date getAddDate() {
		loadWhenNeeded();
		return addDate;
	}

	public String getGroupName() {
		loadWhenNeeded();
		if (group == null) {
			System.out.println("null");
			return "null";
		}
		System.out.println(group.getName());
		return group.getName();
	}

	/**
	 * compute the name of the file and record the contact info
	 * 
	 * @return true on success
	 */
	public boolean saveContact() {
		loadWhenNeeded();
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd_HH.mm.ss");
		String filename = contact.getName() + "&" + contact.getFirstName()
				+ "&" + sdf.format(addDate) + ".contact";
		filename = filename.replaceAll(" ", "_");
		filename = filename.replaceAll(":", ".");
		System.out.println(filename);
		return contact.save(filename);
	}

	/**
	 * load contact from a file
	 * 
	 * @param filename
	 *            the name of the file
	 */
	public void loadContact(String filename) {
		ArrayList<String> content = new ArrayList<String>();
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd_HH.mm.ss");
		try {
			InputStream ips = new FileInputStream(filename);
			InputStreamReader ipsr = new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			String line;
			while ((line = br.readLine()) != null) {
				content.add(line);
			}
			br.close();
			contact = new Contact(content.get(0), content.get(1),
					content.get(2), content.get(3), content.get(4),
					content.get(5), content.get(6), content.get(7),
					content.get(8), content.get(9), content.get(10), "");
			addDate = sdf.parse(filename.split("[&]")[2]);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Error : " + e);
			System.out.println(e.toString());
			e.printStackTrace();
		}
	}

	/**
	 * load contact from a file with specified date
	 * 
	 * @param filename
	 *            the name of the file
	 * @param date
	 *            the date when the contact was created
	 */
	public void loadContact(String filename, Date date) {
		ArrayList<String> content = new ArrayList<String>();
		try {
			InputStream ips = new FileInputStream(filename);
			InputStreamReader ipsr = new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			String line;
			while ((line = br.readLine()) != null) {
				content.add(line);
			}
			br.close();
			contact = new Contact(content.get(0), content.get(1),
					content.get(2), content.get(3), content.get(4),
					content.get(5), content.get(6), content.get(7),
					content.get(8), content.get(9), content.get(10), "");
			addDate = date;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Error : " + e);
			System.out.println(e.toString());
			e.printStackTrace();
		}
	}

	public void loadWhenNeeded() {
		if (!isLoadInMemory) {
			if (date != null)
				loadContact(filename, date);
			else
				loadContact(filename);
		}
	}

}
